import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router) //让vue使用vue-router插件

//路由懒加载 
//主要是为了解决  进入首页不用一次加载过多资源造成用时过长！！！
let router =  new Router({
  mode:"hash",
  routes: [
    {path:"/",redirect:"/home"},
    {path:"/home",component:()=>import("@/views/Home")},
    {path:"/list",component:()=>import("@/views/List"),children:[
      //二级路由path不能加/
      {path:"",redirect:"guonei"},
      {name:"gn",path:"guonei",component:()=>import("@/views/Guonei")},  
      {name:"gj",path:"guoji",component:()=>import("@/views/Guoji")}
    ]},
    {path:"/mine",component:()=>import("@/views/Mine"),beforeEnter(to,from,next){
      console.log("进入到mine组件了！")
      next()
    }},
    {name:"detail",path:"/detail/:id",component:()=>import("@/views/Detail")}
  ]
})

//全局路由守卫(beforeEach:全局的前置路由)
//to: Route: 即将要进入的目标 路由对象
//from: Route: 当前导航正要离开的路由
/* router.beforeEach((to,from,next)=>{
  console.log("全局前置路由守卫...")
  if(from.path==="/home"){
    alert("从首页离开了哦...")
  }
  // 一定要调用该方法来 resolve 这个钩子
  next()
}) */

//全局后置钩子
// router.afterEach((to,from)=>{
//   if(to.path==="/mine"){
//     alert("进入到mine了！")
//   }
// })



//局部 （路由独享的守卫）(组件内的守卫)

export default router;